/**************************************************************************/
/*  Copyright (C) School of Software Engineering of USTC, 2012             
/*                                                                        
/*  FILE NAME: tchdbex.c                                                  
/*  PRINCIPAL AUTHOR: SA12226114(Xin Zhu), SA12226139(Dan Shu)            
/*  SUBSYSTEM NAME: DB                                                   
/*  MODULE NAME: dbapi                                                   
/*  LANGUAGE: C                                                          
/*  TARGET ENVIRONMENT: Linux                                             
/*  DATE OF FIRST RELEASE: 2012-11-20                                    
/*  DESCRIPTION: Impement of Abstract Interface for Tokyo Cabinet DB API                        
/**************************************************************************/

#include "common.h"
#include "dbapi.h"


/*****************************************************************
Function Name: main
Description: This is a main function
Parameter:  

@argc: argument count
@argv: argument vector

Author: SA12226114(Xin Zhu), SA12226139(Dan Shu)
Date: 2012-11-27
******************************************************************/
int main(int argc, char *argv[])
{
    void *db = NULL;
    char cmdline[CMDLINE_LEN], cmd[CMD_LEN], key[KEY_LEN], value[VALUE_LEN];
    
    /* display menu */
    util_menu();

    while(1)
    {
        /* format command line */
        util_format(cmdline, cmd, key, value);
        
        if(strncmp(cmd, "open", sizeof("open")) == 0)                   /* create database */
        {
            db_create(&db, key);
        }
        else if(strncmp(cmd, "set", sizeof("set")) == 0)                /* store records */
        {
            db_write(db, key, value);
        }
        else if(strncmp(cmd, "get", sizeof("get")) == 0)                /* retrieve records */
        {
            db_read(db, key);
        }
        else if(strncmp(cmd, "remove", sizeof("remove")) == 0)          /* remove a record */
        {
            db_remove(db, key);
        }
        else if(strncmp(cmd, "ls", sizeof("ls")) == 0)                  /* dump all records in database */
        {
            db_queryallrecords(db);
        }
        else if(strncmp(cmd, "close", sizeof("close")) == 0)            /* close the database */
        {
            db_close(&db);
        }
        else if(strncmp(cmd, "delete", sizeof("delete")) == 0)          /* delete the object */
        {
            db_delete(&db);
        }
        else if(strncmp(cmd, "help", sizeof("help")) == 0)              /* display menu info */
        {
            util_menu();
        }
        else if(strncmp(cmd, "quit", sizeof("quit")) == 0)              /* quit system */
        {
            return;
        }
            
    }

    return 0;
}


